//
//  RecommendViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/15.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "RecommendViewController.h"
#import "GetDataMethods.h"
#import "UIImageView+WebCache.h"
#import "InformationTableViewCell.h"
#import "PictureViewController.h"
#import "Model.h"
#import "Fit.h"
#import "MBProgressHUD.h"

@interface RecommendViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArray;
@property(nonatomic, retain)NSArray *array;
@property(nonatomic, retain)NSString *string;
@property(nonatomic, assign)NSInteger number;
@property(nonatomic, retain)MBProgressHUD *progressView;
@property(nonatomic, retain)UIView *headView;
@property(nonatomic, retain)UIImageView *topImageView;
@property(nonatomic, retain)UILabel *titleLabel;
@property(nonatomic, retain)UILabel *indexLabel;
@property(nonatomic, retain)UILabel *infoLabel;

@end

@implementation RecommendViewController
-(void)dealloc
{
    [_tableView release];
    [_dataArray release];
    [_array release];
    [_progressView release];
    [_headView release];
    [_topImageView release];
    [_titleLabel release];
    [_indexLabel release];
    [_infoLabel release];
    [super dealloc];
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    self.headView.backgroundColor = [Model shareInstance].BackgroundColor;
    self.titleLabel.textColor = [Model shareInstance].TitleColor;
    self.indexLabel.textColor = [Model shareInstance].TitleColor;
    self.titleLabel.textColor = [Model shareInstance].TitleColor;
    self.infoLabel.textColor = [Model shareInstance].TitleColor;
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _data.name;
    [self changeColor];
}

-(void)loadView
{
    [super loadView];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    [self getData];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //初始化加载中视图
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
}

-(void)getData
{
    self.array = [NSArray arrayWithObjects:@"❀ 概况", @"❀ 地址", @"❀ 到达方式", @"❀ 开放时间", @"❀ 联系方式", nil];
    self.dataArray = [NSMutableArray array];
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/", (long)_data.type, _data.Id] BodyString:nil WithDataBlock:^(id data) {
        [_dataArray addObject:[data objectForKey:@"description"]];
        [_dataArray addObject:[data objectForKey:@"address"]];
        [_dataArray addObject:[data objectForKey:@"arrival_type"]];
        [_dataArray addObject:[data objectForKey:@"opening_time"]];
        [_dataArray addObject:[data objectForKey:@"tel"]];
        self.string = [data objectForKey:@"id"];
        self.number = [[data objectForKey:@"type"]integerValue];
        
        [_tableView reloadData];
        [self createViews];
        [_progressView removeFromSuperview];
    }];
}

-(void)createViews
{
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 400 * H)];
    _headView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    _tableView.tableHeaderView = _headView;
    [_headView release];
    self.topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * H)];
    [_topImageView sd_setImageWithURL:[NSURL URLWithString:_data.cover]];
    [_headView addSubview:_topImageView];
    [_topImageView release];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pictureTapAction)];
    _topImageView.userInteractionEnabled = YES;
    [_topImageView addGestureRecognizer:tap];
    [tap release];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(160 * W, 170 * H, 60 * W, 60 * W)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_data.icon]];
    imageView.layer.cornerRadius = imageView.frame.size.width / 2;
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderWidth = 2;
    imageView.layer.borderColor = [[UIColor whiteColor]CGColor];
    [_headView addSubview:imageView];
    [imageView release];
    self.titleLabel = [[UILabel alloc]initWithFrame: CGRectMake(20 * W, _topImageView.frame.origin.y + _topImageView.frame.size.height + 50 * H, self.view.frame.size.width - 40 * W, 40 * H)];
    _titleLabel.text = _data.name;
    _titleLabel.textAlignment = 1;
    _titleLabel.font = [UIFont systemFontOfSize:25];
    [_headView addSubview:_titleLabel];
    [_titleLabel release];
    self.indexLabel = [[UILabel alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 20 * H, _titleLabel.frame.size.width, 20 * H)];
    _indexLabel.text = _data.recommended_reason;
    _indexLabel.textAlignment = 1;
    [_headView addSubview:_indexLabel];
    [_indexLabel release];
    self.infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(_indexLabel.frame.origin.x, _indexLabel.frame.origin.y + _indexLabel.frame.size.height + 30 * H, _indexLabel.frame.size.width, 30 * H)];
    _infoLabel.text = @"·  基本信息  ·";
    _infoLabel.textAlignment = 1;
    _infoLabel.font = [UIFont systemFontOfSize:20];
    [_headView addSubview:_infoLabel];
    [_infoLabel release];
    UILabel *xianLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 14 * H, 100 * W, 1 * H)];
    xianLabel.backgroundColor = [UIColor lightGrayColor];
    [_infoLabel addSubview:xianLabel];
    [xianLabel release];
    UILabel *xianLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(_infoLabel.frame.size.width - 100 * W, xianLabel.frame.origin.y, xianLabel.frame.size.width, xianLabel.frame.size.height)];
    xianLabel2.backgroundColor = [UIColor lightGrayColor];
    [_infoLabel addSubview:xianLabel2];
    [xianLabel2 release];
    
    [self changeColor];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[InformationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.infoLabel.text = _dataArray[indexPath.row];
    }
    cell.aLabel.text = _array[indexPath.row];
    cell.aLabel.textColor = [Model shareInstance].TitleColor;
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArray.count != 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
        CGRect rect = [_dataArray[indexPath.row] boundingRectWithSize:CGSizeMake(375 * W, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 70 * H;
    }
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(void)pictureTapAction
{
    PictureViewController *picVC = [[PictureViewController alloc]init];
    picVC.string = self.string;
    picVC.number = _number;
    [self.navigationController pushViewController:picVC animated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
